from django.db import models

from utils.encryption import change_name


# Create your models here.
class Image(models.Model):
    class Meta:
        verbose_name = "图片"
        verbose_name_plural = verbose_name

    images = models.ImageField(max_length=300, upload_to=change_name, null=True, blank=True)
    time = models.DateTimeField(auto_now_add=True)

    def photo_url(self):
        """
        当用户注册时没有上传照片，模板中调用[ModelName].[ImageFieldName].url时赋予一个默认路径
        :return:
        """
        if self.images and hasattr(self.images, 'url'):
            return self.images.url
        else:
            return "/media/avatar/default.png"


class UserInfo(models.Model):
    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    SCHOOL_ZONE = (
        (1, "青山湖校区"),
        (2, "前湖校区"),
        (3, "东湖校区")
    )
    student_id = models.CharField(max_length=128, verbose_name="学号", unique=True, null=False, blank=False)
    user_name = models.CharField(max_length=64, verbose_name="用户名", null=False, blank=False, unique=True)
    avatar = models.OneToOneField(Image, on_delete=models.CASCADE, verbose_name="用户头像")
    password = models.CharField(max_length=256, verbose_name="密码", null=False, blank=False)
    school_zone = models.IntegerField(choices=SCHOOL_ZONE, default=1, verbose_name="校区")
    profile = models.CharField(max_length=200, verbose_name="个人简介", null=True, blank=True)
    qq = models.CharField(max_length=16, verbose_name="QQ", null=False, blank=False)

    def __str__(self):
        return self.user_name


class Tags(models.Model):
    class Meta:
        verbose_name = "标签"
        verbose_name_plural = verbose_name

    tags_content = models.CharField(max_length=32, verbose_name="标签内容")

    def __str__(self):
        return self.tags_content

class Goods(models.Model):
    class Meta:
        verbose_name = "商品"
        verbose_name_plural = verbose_name

    seller = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="卖家", related_name="goods_seller_set")
    goods_title = models.CharField(max_length=128, verbose_name="商品标题")
    goods_price = models.DecimalField(decimal_places=2, max_digits=128, verbose_name="商品价格")
    goods_main_image = models.CharField(max_length=256, null=True, blank=True,  verbose_name="商品主图")
    goods_desc = models.TextField(max_length=1000, verbose_name="商品描述")
    goods_launch_time = models.DateTimeField(auto_now_add=True, verbose_name="商品上架时间")
    goods_is_sold = models.BooleanField(verbose_name="是否已卖出", default=False)
    goods_wanted_person = models.ManyToManyField(UserInfo, related_name="goods_wanted_person_set", verbose_name="买家")
    goods_tags = models.ManyToManyField(Tags, related_name="goods_tags_set", verbose_name="商品标签")

    def __str__(self):
        return self.goods_title


class GoodsAllImage(models.Model):
    class Meta:
        verbose_name = "商品图片"
        verbose_name_plural = verbose_name

    image_path = models.CharField(max_length=256)
    product = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品图片")

    def __str__(self):
        return self.image_path


class Topic(models.Model):
    class Meta:
        verbose_name = "话题"
        verbose_name_plural = verbose_name

    topic_content = models.CharField(max_length=128, verbose_name="话题")


class Thread(models.Model):
    class Meta:
        verbose_name = "帖子"
        verbose_name_plural = verbose_name

    author_info = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="用户信息",
                                    related_name="thread_author_info_set")
    thread_topic = models.ForeignKey(Topic, on_delete=models.CASCADE, verbose_name="所属话题")
    thread_info = models.TextField(max_length=2000, verbose_name="帖子内容")
    thread_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    thread_appreciate = models.IntegerField(verbose_name="点赞数")
    thread_appreciate_peoples = models.ManyToManyField(UserInfo, verbose_name="点赞人员",
                                                       related_name="thread_appreciate_peoples_set")

    def __str__(self):
        return self.thread_topic


class ThreadImage(Image):
    class Meta:
        verbose_name = "帖子图"
        verbose_name_plural = verbose_name

    post = models.ForeignKey(Thread, on_delete=models.CASCADE, verbose_name="帖子图")


# class Token(models.Model):
#     class Meta:
#         verbose_name = "token"
#         verbose_name_plural = verbose_name
#
#     user = models.OneToOneField(UserInfo, on_delete=models.CASCADE, null=True)
#     user_token = models.CharField(max_length=64)
#
#     def __str__(self):
#         return self.user.user_name


class WeChatCertification(models.Model):
    class Meta:
        verbose_name = "微信认证"
        verbose_name_plural = verbose_name

    user = models.OneToOneField(UserInfo, on_delete=models.CASCADE)
    openid = models.CharField(max_length=28, blank=False, null=False)

    def __str__(self):
        return self.user.user_name
